package com.gp.ssmplus.base.core.mybatis.query;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gp.ssmplus.base.core.mybatis.common.utils.FlymeUtils;
import com.gp.ssmplus.base.core.mybatis.common.utils.JsonUtils;
import com.gp.ssmplus.base.core.mybatis.common.utils.ReflectionUtils;
import com.gp.ssmplus.base.core.mybatis.entity.EntityMap;
import com.gp.ssmplus.base.core.mybatis.model.UpdateModel;
import lombok.Data;

/**
 * @author zyf
 */
@Data
public class CriteriaUpdate<T> extends UpdateWrapper<T> {
    private EntityMap requestMap = new EntityMap();

    private String handlerName;

    /**
     * 主键字段
     */
    private String idField;

    private Class<T> cls;

    private UpdateModel model;

    public EntityMap put(String key, Object value) {
        this.requestMap.put(key, value);
        return this.requestMap;
    }

    public CriteriaUpdate() {

    }

    public CriteriaUpdate(UpdateModel model, Class<T> cls) {
        this.handlerName = model.getHandlerName();
        this.model = model;
        this.cls = cls;
        this.idField = ReflectionUtils.getTableField(cls);
        Long id = model.getId();
        String params = model.getParams();
        System.out.println(params);
        if (FlymeUtils.isNotEmpty(params)) {
            this.setRequestMap(JsonUtils.jsonToMap(params, false));
        }
        if (FlymeUtils.isNotEmpty(id)) {
            //给主键赋值
            this.put(idField, id);
            this.eq(idField, id);
        }
    }

    public <T> T getEntity(Class<T> t) {
        return FlymeUtils.getEntity(t, this.getRequestMap());
    }

}
